package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.common.KeyDefinition.NAVIGATION_DEFINITION
import com.benjaminwan.read.free.common.KeyDefinition.NUMERIC_DEFINITION
import com.benjaminwan.read.free.common.KeyDefinition.QWERTY_DEFINITION
import com.benjaminwan.read.free.common.KeyDefinition.findKeyName
import com.benjaminwan.read.free.config.Options.getAction
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.action.KeyboardClick
import com.benjaminwan.read.free.model.action.Position
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.*
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object KeyboardClickConfig {
    fun getKeyboardClickConfig(
        mainVM: MainViewModel, pos: Position
    ): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "键值(手工输入)"
                rightTextFunc = { getAction<KeyboardClick>(mainVM, pos)?.key?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, 3, getRegexFilter(numRegex), { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<KeyboardClick>(mainVM, pos) ?: return@showRightTextInputDialog
                        val key = input.toInt()
                        mainVM.setAction(pos, action.copy(key = key))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "QWERTY区按键选择"
                rightTextFunc = {
                    val key = getAction<KeyboardClick>(mainVM, pos)?.key
                    if (key == null) {
                        "没有设置键值"
                    } else {
                        findKeyName(key, QWERTY_DEFINITION)
                    }
                }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title, QWERTY_DEFINITION
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<KeyboardClick>(mainVM, pos) ?: return@showPicker
                        mainVM.setAction(pos, action.copy(key = opValue))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "Navigation区按键选择"
                rightTextFunc = {
                    val key = getAction<KeyboardClick>(mainVM, pos)?.key
                    if (key == null) {
                        "没有设置键值"
                    } else {
                        findKeyName(key, NAVIGATION_DEFINITION)
                    }
                }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title, NAVIGATION_DEFINITION
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<KeyboardClick>(mainVM, pos) ?: return@showPicker
                        mainVM.setAction(pos, action.copy(key = opValue))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "Numeric区按键选择"
                rightTextFunc = {
                    val key = getAction<KeyboardClick>(mainVM, pos)?.key
                    if (key == null) {
                        "没有设置键值"
                    } else {
                        findKeyName(key, NUMERIC_DEFINITION)
                    }
                }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title, NUMERIC_DEFINITION
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<KeyboardClick>(mainVM, pos) ?: return@showPicker
                        mainVM.setAction(pos, action.copy(key = opValue))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "按下时间(单位ms)"
                rightTextFunc =
                    { getAction<KeyboardClick>(mainVM, pos)?.delayRange?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<KeyboardClick>(mainVM, pos) ?: return@showRightTextInputDialog
                        if (input.isEmpty()) {
                            mainVM.setAction(pos, action.copy(delayRange = (1L..1)))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toLongOrNull()
                            val end = range[1].toLongOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start <= 0L) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)<=0")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(delayRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}